<?php
namespace Shared\LRC\Metadata\Parsers;

/**
 * @package Entity
 * @version 1.0.0.0
 * @author J.Minchev
 * @internal 
 * @todo
 */
class Entity implements ParserI {

    /**
     * @var string
     */
    private $classname;

    /**
     * @var \Doctrine\ORM\EntityManager
     */
    private $EntityManager;

    /**
     * @var \Doctrine\Common\Annotations\Reader
     */
    private $Reader;
    private $Entity;

    /**
     * @var \Shared\LRC\Metadata\EntityMetadata 
     */
    private $EntityMetadata;

    /**
     * @var array
     */
    private $lrcContent;

    public function __construct($Entity, \Doctrine\ORM\EntityManager $EntityManager) {
        $this->Entity = $Entity;
        $this->classname = get_class($Entity);
        $this->EntityManager = $EntityManager;
        
        $this->Reader = \Shared\Application::instance()->getAnnotationReader();
//        $this->Reader = $this->EntityManager->getConfiguration()->getMetadataDriverImpl()->getReader();
    }

    /**
     * @throws \Shared\LRC\Metadata\Parsers\NotPropertyMappedEntityException
     * @return \Shared\LRC\Metata\EntityMetadata
     */
    public function parse() {
        if (!$this->EntityMetadata) {

            $ClassMetadataFactory = $this->EntityManager->getMetadataFactory();
            $ClassMetadata = $ClassMetadataFactory->getMetadataFor($this->classname);

            $EntityAnnotation = $this->Reader->getClassAnnotation($ClassMetadata->getReflectionClass(), '\Shared\LRC\Annotations\Entity');
            
            if (!$EntityAnnotation) {
                throw new NotPropertyMappedEntityException('No LRC\Annotations\Entity class annotation found for class ' . $this->classname);
            }

            $lrcProperties = array();
			$lrcAssociations = array();
            foreach ($ClassMetadata->getReflectionClass()->getProperties()as $Property) {
                $propertyAnnotations = $this->Reader->getPropertyAnnotations($Property);
                foreach ($propertyAnnotations as $PropertyAnnotation) {
                    if ($PropertyAnnotation instanceof \Shared\LRC\Annotations\Translation) {
                        $translationPropertyName = $Property->name;
                        $translationEntityClass = $ClassMetadata->associationMappings[$Property->name]['targetEntity'];
                    } else if ($PropertyAnnotation instanceof \Shared\LRC\Annotations\Column) {
                        $lrcProperties[] = $Property->name;
                    }
                    else if ($PropertyAnnotation instanceof \Shared\LRC\Annotations\Assoc) {
                        $lrcAssociations[] = $Property->name;
                    }
                }
            }
            if (!$translationPropertyName || !$translationEntityClass) {
                throw new NotPropertyMappedEntityException('No LRC\Annotations\Collection property annotation found in ' . $this->classname);
            } else if (count($lrcProperties) == 0 and count($lrcAssociations) == 0) {
                throw new NotPropertyMappedEntityException('No LRC\Annotations\Column property annotation found in ' . $this->classname);
            }
            $this->EntityMetadata = new \Shared\LRC\Metadata\EntityMetadata($translationPropertyName, $translationEntityClass, $lrcProperties);
			$this->EntityMetadata->setLrcAssociations($lrcAssociations);
        }
        return $this->EntityMetadata;
    }



}